import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
import 'package:get/get_rx/src/rx_workers/utils/debouncer.dart';

import '../generated/assets.dart';

enum ToastType { success, warn, error }

/// @description :get 扩展函数
extension GetExtension on GetInterface {

  static FToast fToast = FToast()..init(Get.context!);

  static final debouncerCallback = Debouncer(delay: 500.milliseconds);

  Future showToast({
    String content = '',
    double opacity = 0.8,
    Alignment alignment = Alignment.topCenter,
    ToastType toastType = ToastType.success,
    VoidCallback? disappearCallBack,
    Duration? toastDuration,
    deBouncer = false,
  }) async {
    var icon = toastType == ToastType.success
        ? Assets.svgToastSuccess
        : toastType == ToastType.warn
            ? Assets.svgToastWarn
            : Assets.svgToastError;
    Widget toast = Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.h),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(72.0),
        color: Colors.black.withOpacity(0.8),
      ),
      width: double.infinity,
      child: Text.rich(
        TextSpan(children: [
          WidgetSpan(
            child: SvgPicture.asset(
              icon,
              width: 18.w,
              height: 18.w,
            ).paddingOnly(right: 10.w),
          ),
          TextSpan(
            text: content,
            style: Get.textTheme.bodyMedium?.copyWith(
                color: Colors.white, fontSize: 16.sp, height: 22 / 16),
          )
        ]),
        textAlign: TextAlign.center,
      ),
    );

    if (deBouncer) {
      debouncerCallback(() {
        fToast.showToast(
            child: toast,
            toastDuration: toastDuration ?? 1.seconds,
            gravity: ToastGravity.BOTTOM,
            positionedToastBuilder: (_, child) =>
                Positioned(top: 40.h, left: 36.w, right: 36.w, child: child));
      });
    } else {
      fToast.showToast(
          child: toast,
          toastDuration: toastDuration ?? 1.seconds,
          gravity: ToastGravity.BOTTOM,
          positionedToastBuilder: (_, child) =>
              Positioned(top: 40.h, left: 36.w, right: 36.w, child: child));
    }
  }
}
